package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.BeanLoginResultBody;
import com.haiyi.software.entity.UserInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

public abstract class ActivityPersonalBasicFollow extends ActivityPersonalBasic {

	private static final String str_requestUrlFollow = "commGz.php";

	protected abstract void followReturn(JsonElement jsonReturn);

	private String user_uid = null;

	private String user_uname = null;

	private Boolean isWork = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	public Boolean getIsWork() {

		return isWork;
	}

	/**
	 * 关注
	 * 
	 * @param uname
	 */
	protected void setFollow(String uid, String uname) {

		if (!isWork) {

			this.user_uid = uid;

			this.user_uname = uname;

			AsyncHttpClient client = MainApplication.getInstance()
					.getAsyncHttpClient();

			RequestParams params = new RequestParams();

			params.put("args", getJsonFollowArgs());

			client.post(str_server + str_requestUrlFollow, params, rpHandler);
		}

	}

	private ResponseHandlerInterface rpHandler = new AsyncHttpResponseHandler() {

		JsonElement jsonReturn = null;

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {

			isWork = false;

			try {
				String str_jsonReturn = new String(responseBody, "UTF-8");

				Log.i("rbody", str_jsonReturn);

				JsonParser parse = new JsonParser();

				jsonReturn = parse.parse(str_jsonReturn);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

			Log.i("success", statusCode + "");

			followReturn(jsonReturn);
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			isWork = false;
			requestError();
		}
	};

	/**
	 * 返回关注的参数字符串
	 * 
	 * @return
	 */
	private String getJsonFollowArgs() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();
		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> body = new HashMap<String, Object>();
			// body.put("lat", myInfo.getLat());
			// body.put("longitude", myInfo.getLongitude());
			body.put("uid", this.user_uid);
			body.put("uname", this.user_uname);
			// TODO:分页
			// body.put("page", 1);
			// body.put("percount", 999999);

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", body);

			Gson gson = new Gson();
			String s = gson.toJson(args);

			Log.i("argsJson", s);
			return s;
		} else {
			return "";
		}
	}
}
